<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.8/dist/vue.js" type="text/javascript"></script>
</head>
<body>
    <div id="box">
        <h2>学校信息</h2>
        <h3>学校名称：{{school}}</h3>
        <h3>学校地址：{{address}}</h3>
        <h2>学生信息</h2>
        <button @click="addSex">点击按钮添加一个性别属性-默认值为男</button>
        <button @click="students.sex='保密' ">修改性别为保密</button>
        <h3>姓名：{{students.name}}</h3>
        <h3 v-if="students.sex">性别：{{students.sex}}</h3>
        <h3>年龄：{{students.age}}</h3>
        <h3>学号：{{students.number}}</h3>
        <h3>爱好</h3>
        <button @click="changeHobby">修改第一个爱好为地下城</button>
        <button @click="addHobby">添加一个爱好</button>
        <ul>
            <li v-for="(h,index) in students.hobby" :key="index">
               {{index+1}} --{{h}}
            </li>
        </ul>
        <h2>friends</h2>
        <button @click="addFriend">添加一位朋友</button>
        <button @click="changeName">修改第二个朋友的名字为宇</button>
        <ul>
            <li v-for="(f,index) in students.friends" :key="index">
                {{f.name}}--{{f.age}}
            </li>
        </ul>
    </div>

    <script>
        Vue.config.productionTip = false;  //阻止vue在启动时生成生产提示
        //创建实例
        let vm = new Vue({
            el:"#box",
            data:{
                school:"天津商业大学",
                address:"天津市",
                students:{
                    name:"苏凉",
                    age:21,
                    number:20194124,
                    hobby:['LOL','飞车','原神'],
                    friends:[
                        {name:"lucy",age:21},
                        {name:"ming",age:22}
                    ]
                }   
            },
            methods: {
                addSex(){
                    Vue.set(this.students,'sex','男')
                },
                addFriend(){
                    // Vue.set(this.students.friends,2,{name:"lina",age:21})
                    this.students.friends.push({name:"lina",age:21})
                },
                changeName(){
                    this.students.friends[1].name = '宇'
                },
                changeHobby(){
                    // this.students.hobby.splice(0,1,'地下城')
                    Vue.set(this.students.hobby,0,'地下城')
                },
                addHobby(){
                    this.students.hobby.push("开车")
                }
            },
        })
    </script>
</body>
</html>